<?php
//echo "<pre>";
//print_r($_GET["callback"]);
//echo "</pre>";
//die;
//$data = [
//    "data" => [
//        [
//            'id' => '1',
//            'areaname' => "北京",
//            'pid' => '0',
//            'shortname' => "北京",
//            'level' => "1",
//            'position' => "tr_0",
//            'sort' => "1",
//        ],
//        [
//            'id' => '2',
//            'areaname' => "北京1",
//            'pid' => '1',
//            'shortname' => "北京1",
//            'level' => "2",
//            'position' => "tr_0",
//            'sort' => "1",
//        ],
//    ],
//    'state' => 1,
//];
//
//echo $_GET["callback"]."(".json_encode($data, JSON_FORCE_OBJECT).")";
//die;
//CREATE TABLE `baidupantree` (
//`id` int(10) NOT NULL AUTO_INCREMENT,
//  `pid` int(10) DEFAULT '0',
//  `level` int(5) DEFAULT '1',
//  `name` varchar(255) DEFAULT NULL,
//  `content` text,
//  `sort` tinyint(5) DEFAULT NULL,
//  `delete` int(10) DEFAULT NULL,
//  PRIMARY KEY (`id`)
//) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=utf8;
/*$config = [
    "dsn" => "mysql:dbname=huiju;host=localhost",
    "username" => "root",
    "password" => "root",
    "charset" => "utf8",
];
$db = new DB();
$db->__setup($config);*/
$c = $_GET["c"];
$a = $_GET["a"];
$data = call_user_func([new $c,$a]);


class bdptree
{
    protected $config = [
        "dsn" => "mysql:dbname=huiju;host=1.117.208.136",
        "username" => "kmysql",
        "password" => "FDsa$#21$#",
        "charset" => "utf8",
    ];
    protected $db;

    public function __construct()
    {
        $this->db = new DB();
        $this->db->__setup($this->config);
    }

    public function index()
    {
        $data = $this->childrens(0);
        echo json_encode($data);
        die;
    }

    public function add()
    {
        $params = $_REQUEST;
        $pid = $params["pid"] > 0 ? $params["pid"] : 0;
        $level = 0;

        if($pid > 0){
            $sql = "SELECT * FROM baidupantree WHERE id='$pid'";
            $result = $this->db->fetch($sql);
            $level = $result["level"]+1;
        }

        $rows = explode("\n",$_REQUEST["data"]);

        foreach ($rows as $name) {
            if(!empty(trim($name))){
                $data = [
                    "pid" => $pid,
                    "level" => $level,
                    "name" => $name,
                    "content" => $name,
                    "sort" => 55,
                ];
                $this->db->insert('baidupantree',$data);
            }
        }

        echo "bdptree|add";
    }

    public function edit()
    {
        echo "bdptree|edit";
    }

    public function del()
    {
        echo "bdptree|del";
    }

    private function childrens($level=0,$pid=null){
        $where = "level='$level'";
        if($pid){
            $where .= " AND pid='$pid' ";
        }
        $sql = "SELECT * FROM baidupantree WHERE $where ORDER BY sort DESC";
        $result = $this->db->fetchAll($sql);
        foreach ($result as $k => $v) {
            $result[$k]["child"] = $this->childrens($level+1,$v["id"]);
        }

        return $result;
    }

}
/**
 * User: Salamander
 * Date: 2016/9/2
 * Time: 9:16
 */
class DB
{
    private $dsn;
    private $sth;
    private $dbh;
    private $user;
    private $charset;
    private $password;

    public $lastSQL = '';

    public function __setup($config = array())
    {
        $this->dsn = $config['dsn'];
        $this->user = $config['username'];
        $this->password = $config['password'];
        $this->charset = $config['charset'];
        $this->connect();
    }

    private function connect()
    {
        if (!$this->dbh) {
            $options = array(
                \PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES ' . $this->charset,
            );
            $this->dbh = new \PDO($this->dsn, $this->user,
                $this->password, $options);
        }
    }

    public function beginTransaction()
    {
        return $this->dbh->beginTransaction();
    }

    public function inTransaction()
    {
        return $this->dbh->inTransaction();
    }

    public function rollBack()
    {
        return $this->dbh->rollBack();
    }

    public function commit()
    {
        return $this->dbh->commit();
    }

    function watchException($execute_state)
    {
        if (!$execute_state) {
            throw new MySQLException("SQL: {$this->lastSQL}\n" . $this->sth->errorInfo()[2], intval($this->sth->errorCode()));
        }
    }

    public function fetchAll($sql, $parameters = [])
    {
        $result = [];
        $this->lastSQL = $sql;
        $this->sth = $this->dbh->prepare($sql);
        $this->watchException($this->sth->execute($parameters));
        while ($result[] = $this->sth->fetch(\PDO::FETCH_ASSOC)) {
        }
        array_pop($result);
        return $result;
    }

    public function fetchColumnAll($sql, $parameters = [], $position = 0)
    {
        $result = [];
        $this->lastSQL = $sql;
        $this->sth = $this->dbh->prepare($sql);
        $this->watchException($this->sth->execute($parameters));
        while ($result[] = $this->sth->fetch(\PDO::FETCH_COLUMN, $position)) {
        }
        array_pop($result);
        return $result;
    }

    public function exists($sql, $parameters = [])
    {
        $this->lastSQL = $sql;
        $data = $this->fetch($sql, $parameters);
        return !empty($data);
    }

    public function query($sql, $parameters = [])
    {
        $this->lastSQL = $sql;
        $this->sth = $this->dbh->prepare($sql);
        $this->watchException($this->sth->execute($parameters));
        return $this->sth->rowCount();
    }

    public function fetch($sql, $parameters = [], $type = \PDO::FETCH_ASSOC)
    {
        $this->lastSQL = $sql;
        $this->sth = $this->dbh->prepare($sql);
        $this->watchException($this->sth->execute($parameters));
        return $this->sth->fetch($type);
    }

    public function fetchColumn($sql, $parameters = [], $position = 0)
    {
        $this->lastSQL = $sql;
        $this->sth = $this->dbh->prepare($sql);
        $this->watchException($this->sth->execute($parameters));
        return $this->sth->fetch(\PDO::FETCH_COLUMN, $position);
    }

    public function update($table, $parameters = [], $condition = [])
    {
        $table = $this->format_table_name($table);
        $sql = "UPDATE $table SET ";
        $fields = [];
        $pdo_parameters = [];
        foreach ($parameters as $field => $value) {
            $fields[] = '`' . $field . '`=:field_' . $field;
            $pdo_parameters['field_' . $field] = $value;
        }
        $sql .= implode(',', $fields);
        $fields = [];
        $where = '';
        if (is_string($condition)) {
            $where = $condition;
        } else if (is_array($condition)) {
            foreach ($condition as $field => $value) {
                $parameters[$field] = $value;
                $fields[] = '`' . $field . '`=:condition_' . $field;
                $pdo_parameters['condition_' . $field] = $value;
            }
            $where = implode(' AND ', $fields);
        }
        if (!empty($where)) {
            $sql .= ' WHERE ' . $where;
        }
        return $this->query($sql, $pdo_parameters);
    }

    public function insert($table, $parameters = [])
    {
        $table = $this->format_table_name($table);
        $sql = "INSERT INTO $table";
        $fields = [];
        $placeholder = [];
        foreach ($parameters as $field => $value) {
            $placeholder[] = ':' . $field;
            $fields[] = '`' . $field . '`';
        }
        $sql .= '(' . implode(",", $fields) . ') VALUES (' . implode(",", $placeholder) . ')';

        $this->lastSQL = $sql;
        $this->sth = $this->dbh->prepare($sql);
        $this->watchException($this->sth->execute($parameters));
        $id = $this->dbh->lastInsertId();
        if (empty($id)) {
            return $this->sth->rowCount();
        } else {
            return $id;
        }
    }

    public function errorInfo()
    {
        return $this->sth->errorInfo();
    }

    protected function format_table_name($table)
    {
        $parts = explode(".", $table, 2);

        if (count($parts) > 1) {
            $table = $parts[0] . ".`{$parts[1]}`";
        } else {
            $table = "`$table`";
        }
        return $table;
    }

    function errorCode()
    {
        return $this->sth->errorCode();
    }
}

class MySQLException extends \Exception
{
}